<?php

/**
 * Controller_teaching
 * 公开课
 */

class Controller_open extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->subject  = YOU::loadModel('subject');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->log      = YOU::loadModel('log');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 公开课安排列表 **/
	public function actionOpenList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		// 获取当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_class_open` WHERE semester_id ={$semester_id}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$open_list = $this->db->fetchAll("SELECT * FROM `tbl_class_open` WHERE semester_id ={$semester_id} ORDER BY open_time LIMIT {$start}, ".PAGE_LIMIT."");
		foreach($open_list as $key=>$value){
			// 教师名称（提交者）
			$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
			$open_list[$key]['staff_name'] = $staff_data['staff_name'];
			// 班级
			$class_data = $this->db->fetchRow("SELECT `class_name` FROM `tbl_class` WHERE class_id={$value['class_id']}");
			$open_list[$key]['class_name'] = $class_data['class_name'];
			// 学科
			$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id={$value['subject_id']}");
			$open_list[$key]['subject_name'] = $subject_data['subject_name'];
			// 自动调用周次

			$week = date("N",strtotime($value['open_time']));//星期
			$open_list[$key]['open_week'] = $this->week_ini[$week];
			// 转时间格式
			$open_list[$key]['open_time'] = date("m-d",strtotime($value['open_time']));
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("open_list", $open_list);
		$this->tpl->assign("semester_data", $semester_data);
		$this->tpl->display('open/list.tpl.php');
	}

	/** 导出数据 **/
	public function actionOutData()
	{
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$open_list = $this->db->fetchAll("SELECT * FROM `tbl_class_open` WHERE semester_id ={$semester_id} ORDER BY open_week_num,open_time DESC");
		foreach($open_list as $key=>$value){
			// 教师名称（提交者）
			$staff_name = $this->model->staff->getNameById($value['staff_id']);
			// 班级
			$class_name = $this->model->class->getNameById($value['class_id']);

			// 学科
			$subject_name = $this->model->subject->getNameById($value['subject_id']);
			// 自动调用周次

			$week = date("N",strtotime($value['open_time']));//星期
			$open_week = $this->week_ini[$week];
			// 转时间格式
			$open_time = date("m-d",strtotime($value['open_time']));


			$info = array(
			"0" => $value['open_week_num'],
			"1" => $staff_name,
			"2" => $open_time,
			"3" => $open_week,
			"4" => $value['open_num'],
			"5" => $class_name,
			"6" => $value['open_content'],
			"7" => $subject_name,
			"8" => $value['open_remark'],
			);


			$data_lists[] = $info;


		}


		// 模板标题
		$this->model->excel->excel_ini['open']['title'] = "学校{$semester_data['semester_name']}公开课安排表";
		$this->model->excel->outData("open", $data_lists);


	}


	/** 公开课安排添加 **/
	public function actionOpenAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		// 全部班级
		$class_list		= $this->db->fetchAll("SELECT `class_name`,`class_id` FROM `tbl_class_up` WHERE is_now=1 ORDER BY class_code");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 全部老师
		$staff_list		= $this->db->fetchAll("SELECT `staff_name`,`staff_id` FROM `tbl_staff`");	// 获取当前学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if ($_POST) {
			//var_dump($_POST);exit;
			foreach ($_POST['data'] as $key =>$value)
			{

				$open_week_num = $value['open_week_num'];
				$staff_id = $value['staff_id'];
				$open_time = $value['open_time'];
				$open_week = $value['open_week'];
				$open_num = $value['open_num'];
				$class_id = $value['class_id'];
				$open_content = $value['open_content'];
				$subject_id = $value['subject_id'];
				$open_remark = $value['open_remark'];
				if(empty($open_time)){
					$open_time = date(time());
				}

				// 写入数据库
				$info = array(

				"open_week_num" => $open_week_num,
				"staff_id" => $staff_id,
				"semester_id" => $semester_id,
				"open_time" => $open_time,
				"open_week" => $open_week,
				"open_num" => $open_num,
				"class_id" => $class_id,
				"open_content" => $open_content,
				"subject_id" => $subject_id,
				"open_remark" => $open_remark,
				"status" => 1,
				"create_time" => date("Y-m-d H:i:s"),
				"update_time" => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_class_open", $info);

			}Helper_Client::location("添加成功", "index.php?a=open.openlist");
		}

		$this->tpl->assign("class_list", $class_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("staff_list", $staff_list);
		$this->tpl->display("open/add.tpl.php");
	}
	/** 公开课安排批量修改 **/
	public function actionupdateall()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
		// 公开课

		// 全部班级
		$class_list		= $this->db->fetchAll("SELECT `class_name`,`class_id` FROM `tbl_class_up` WHERE is_now =1");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 全部老师
		$staff_list		= $this->db->fetchAll("SELECT `staff_name`,`staff_id` FROM `tbl_staff`");
		$open_lists		= $this->db->fetchAll("SELECT * FROM `tbl_class_open`");



		if ($_POST){
			//var_dump($_POST);exit;

			if($_POST['up'])foreach ($_POST['up'] as $key =>$value){
				$open_week_num = $value['open_week_num'];
				$staff_id = $value['staff_id'];
				$open_time = $value['open_time'];
				$open_week = $value['open_week'];
				$open_num = $value['open_num'];
				$class_id = $value['class_id'];
				$open_content = $value['open_content'];
				$subject_id = $value['subject_id'];
				$open_remark = $value['open_remark'];

				// 写入数据库
				$info = array(

				"open_week_num"       	=> $open_week_num,
				"open_week"       	=> $open_week,
				"staff_id"       		=> $staff_id,
				"open_time"  			=> $open_time,
				"open_num"       		=> $open_num,
				"class_id"  			=> $class_id,
				"open_content"       	=> $open_content,
				"subject_id"       		=> $subject_id,
				"open_remark"  			=> $open_remark,
				"semester_id" => $semester_id,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_update("tbl_class_open", $info, "open_id='{$value['open_id']}'");
			}
			if($_POST['data'])foreach ($_POST['data'] as $key =>$value)
			{

				$open_week_num = $value['open_week_num'];
				$staff_id = $value['staff_id'];
				$open_time = $value['open_time'];
				$open_week = $value['open_week'];
				$open_num = $value['open_num'];
				$class_id = $value['class_id'];
				$open_content = $value['open_content'];
				$subject_id = $value['subject_id'];
				$open_remark = $value['open_remark'];

				// 写入数据库
				$info = array(

				"open_week_num" => $open_week_num,
				"staff_id" => $staff_id,
				"semester_id" => $semester_id,
				"open_time" => $open_time,
				"open_week" => $open_week,
				"open_num" => $open_num,
				"class_id" => $class_id,
				"open_content" => $open_content,
				"subject_id" => $subject_id,
				"open_remark" => $open_remark,
				"status" => 1,
				"create_time" => date("Y-m-d H:i:s"),
				"update_time" => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_class_open", $info);

			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=open.openlist");

		}

		$this->tpl->assign("staff_list", $staff_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("class_list", $class_list);
		$this->tpl->assign("open_lists", $open_lists);

		$this->tpl->display("open/updateall.tpl.php");
	}
	/** 公开课安排修改 **/
	public function actionOpenUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$open_id		= (int)$this->get->open_id;

		// 公开课

		// 全部班级
		$class_list		= $this->db->fetchAll("SELECT `class_name`,`class_id` FROM `tbl_class`");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");
		// 全部老师
		$staff_list		= $this->db->fetchAll("SELECT `staff_name`,`staff_id` FROM `tbl_staff`");
		$open_data		= $this->db->fetchRow("SELECT * FROM `tbl_class_open` WHERE open_id={$open_id}");
		if (empty($open_data)){
			Helper_Client::alert("公开课不存在");
		}
		$uid = $this->user['user_id'];
		if($open_data['user_id']&& ($uid !=$open_data['user_id']) ){
			Helper_Client::alert("对不起无此权限");
			exit;
		}

		if ($_POST){

			$open_week_num     		= $this->post->open_week_num;
			$staff_id     			= $this->post->staff_id;
			$open_time     			= $this->post->open_time;
			$open_num     			= $this->post->open_num;
			$class_id     			= $this->post->class_id;
			$open_content     	    = $this->post->open_content;
			$subject_id     	    = $this->post->subject_id;
			$open_remark     	    = $this->post->open_remark;

			// 写入数据库
			$info = array(

			"open_week_num"       	=> $open_week_num,
			"staff_id"       		=> $staff_id,
			"open_time"  			=> $open_time,
			"open_num"       		=> $open_num,
			"class_id"  			=> $class_id,
			"open_content"       	=> $open_content,
			"subject_id"       		=> $subject_id,
			"open_remark"  			=> $open_remark,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_class_open", $info, "open_id='{$open_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=open.openlist");

		}

		$this->tpl->assign("staff_list", $staff_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("class_list", $class_list);
		$this->tpl->assign("open_data", $open_data);

		$this->tpl->display("open/update.tpl.php");
	}
	/** 公开课删除 **/
	public function actionOpenDel()
	{
		$open_id = $this->get->open_id;
		$open_data		= $this->db->fetchRow("SELECT * FROM `tbl_class_open` WHERE open_id={$open_id}");
		if (empty($open_data)){
			Helper_Client::alert("公开课不存在");
		}
		$uid = $this->user['user_id'];
		if($open_data['user_id']&& ($uid !=$open_data['user_id']) ){
			Helper_Client::alert("对不起无此权限");
			exit;
		}
		$this->db->query("DELETE  FROM `tbl_class_open` WHERE open_id='{$open_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=open.openlist");
	}
}

?>